package com.xmtlxx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysCheckType;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.TreeBuildUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.bo.SysCheckTypeBo;
import com.xmtlxx.system.domain.vo.SysCheckTypeVo;
import com.xmtlxx.system.mapper.SysCheckTypeMapper;
import com.xmtlxx.system.service.ISysCheckTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 检查项类型Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-04-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysCheckTypeServiceImpl implements ISysCheckTypeService {

    private final SysCheckTypeMapper baseMapper;

    /**
     * 查询检查项类型
     *
     * @param typeId 检查项类型主键
     * @return 检查项类型
     */
    @Override
    public SysCheckTypeVo queryById(Long typeId){
        return baseMapper.selectVoById(typeId);
    }

    /**
     * 查询检查项类型列表
     *
     * @param bo 检查项类型
     * @return 检查项类型
     */
    @Override
    public TableDataInfo<SysCheckTypeVo> queryPageList(SysCheckTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysCheckType> lqw = buildQueryWrapper(bo);
        Page<SysCheckTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检查项类型列表
     *
     * @param bo 检查项类型
     * @return 检查项类型
     */
    @Override
    public List<SysCheckTypeVo> queryList(SysCheckTypeBo bo) {
//        LambdaQueryWrapper<SysCheckType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectSysCheckTypeList(bo);
    }

    private LambdaQueryWrapper<SysCheckType> buildQueryWrapper(SysCheckTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysCheckType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysCheckType::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysCheckType::getStatus, bo.getStatus());
        lqw.eq(bo.getParentId() != null, SysCheckType::getParentId, bo.getParentId());
        return lqw;
    }

    /**
     * 新增检查项类型
     *
     * @param bo 检查项类型
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysCheckTypeBo bo) {
        SysCheckType add = BeanUtil.toBean(bo, SysCheckType.class);
        validEntityBeforeSave(add);

        if(bo.getParentId() != null){
            SysCheckType parent = baseMapper.selectById(bo.getParentId());
            add.setAncestors(parent.getAncestors()+","+bo.getParentId());
        }else{
            add.setParentId(0l);
            add.setAncestors("0");
        }
        //填充UUID
//        add.setTypeId(IdUtil.simpleUUID());


        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeId(add.getTypeId());
        }
        return flag;
    }

    /**
     * 修改检查项类型
     *
     * @param bo 检查项类型
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysCheckTypeBo bo) {
        SysCheckType update = BeanUtil.toBean(bo, SysCheckType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysCheckType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检查项类型
     *
     * @param ids 需要删除的检查项类型主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for(Long id : ids){
            SysCheckType enty = baseMapper.selectById(id);
            enty.setStatus("1");
            baseMapper.updateById(enty);
        }
        return true;
    }

    @Override
    public List<Tree<Long>> buildTreeSelect(List<SysCheckTypeVo> types) {
        if (CollUtil.isEmpty(types)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(types,(type,tree)->
            tree.setId(type.getTypeId())
                .setParentId(type.getParentId())
                .setName(type.getName())
                .setWeight(type.getSort()));
    }
}
